/*
 * Copyright (c) 2013, Klaus Hauschild
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, this list of conditions and
 *     the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *     and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package de.hauschild.worldcupn.core.match;

import playn.core.GroupLayer;
import playn.core.PlayN;
import playn.core.util.Callback;
import pythagoras.f.Point;
import de.hauschild.utilsn.core.Entity;
import de.hauschild.utilsn.core.sprite.NullSprite;
import de.hauschild.utilsn.core.sprite.Sprite;
import de.hauschild.utilsn.core.sprite.Sprites;
import de.hauschild.utilsn.core.sprite.Sprites.AnimationDescription;

/**
 * @since 0.1.0
 * 
 * @author Klaus Hauschild
 */
public class Player implements Entity {

  private Sprite sprite = NullSprite.INSTANCE;
  private Facing facing = Facing.RIGHT;
  private final Point position;
  private final Point move = new Point();

  public Player(final GroupLayer layer, final String team, final String player, final Point position) {
    this.position = position;
    Sprites.load(String.format("images/teams/%s/%s", team, player), new AnimationDescription[] {
        new AnimationDescription(PlayerSpritePhase.IDLE_RIGHT, 1, 150),
        new AnimationDescription(PlayerSpritePhase.IDLE_LEFT, 1, 150),
        new AnimationDescription(PlayerSpritePhase.WALK_RIGHT, 2, 150),
        new AnimationDescription(PlayerSpritePhase.WALK_LEFT, 2, 150),
    }, new Callback<Sprite>() {

      @Override
      public void onFailure(final Throwable cause) {
        PlayN.log().error(String.format("Unable to load sprite of %s (%s).", player, team), cause);
      }

      @Override
      public void onSuccess(final Sprite result) {
        sprite = result;
        sprite.getLayer().setOrigin(31.5f, 99f);
        updateDepth();
        layer.addAt(sprite.getLayer(), position.x, position.y);
      }

    });
  }

  public void down() {
    updateDepth();
    updateMove(0, 1);
  }

  public void left() {
    updateMove(-1, 0);
    facing = Facing.LEFT;
  }

  @Override
  public void paint(final float alpha) {
    sprite.getLayer().setTranslation(position.x, position.y);
    sprite.paint(alpha);
  }

  public void right() {
    updateMove(1, 0);
    facing = Facing.RIGHT;
  }

  public void up() {
    updateDepth();
    updateMove(0, -1);
  }

  @Override
  public void update(final int delta) {
    final float speed = delta / 2f;
    position.addLocal(move.x * speed, move.y * speed);
    if (move.x == 0 && move.y == 0) {
      switch (facing) {
      case RIGHT:
        sprite.setPhase(PlayerSpritePhase.IDLE_RIGHT);
        break;
      case LEFT:
        sprite.setPhase(PlayerSpritePhase.IDLE_LEFT);
        break;
      }
    } else {
      switch (facing) {
      case RIGHT:
        sprite.setPhase(PlayerSpritePhase.WALK_RIGHT);
        break;
      case LEFT:
        sprite.setPhase(PlayerSpritePhase.WALK_LEFT);
        break;
      }

    }
    move.x = 0;
    move.y = 0;
    sprite.update(delta);
  }

  private void updateDepth() {
    sprite.getLayer().setDepth(position.y);
  }

  private void updateMove(final int x, final int y) {
    if (move.x != x && x != 0) {
      move.x = x;
    }
    if (move.y != y && y != 0) {
      move.y = y;
    }
  }

}
